<?php 
	include("confirmationAlert.php");
?>

<div id="edit-event-keeper" style="font-size: 62.5%; display: none;"
	title="Edycja rezerwacji">
	<form id="edit-event-form" method="post"
		action="<?php echo Yii::app()->request->baseUrl; ?>/index.php/calendar/editKeeperReservation">
		<label for="station" style='color: blue;'><b>Stanowisko: </b> </label>
		<select id="edit-event-station" name="station"></select> <span
			style='display: block; margin-top: 10px; color: blue;'><b>Termin
				rezerwacji:</b> </span> <label>Czas: </label> <input id="starttime"
			class="starttime" name="starttime" size="4" style='display: inline;'>
		<label for="station"> - </label> <input id="endtime" class="endtime"
			name="endtime" size="4" style='display: inline;'> <label
			style='margin-left: 20px;'>Data: </label> <input id="reservationdate"
			class="reservationdate" name="reservationdate" size="9"
			style='display: inline;'> <span
			style='display: block; margin-top: 10px; color: blue;'><b>Wybierz
				narzędzia:</b> </span>
		<div id="edit-event-avail-tools"></div>
		<input id="maintanace" class="maintanace" name="maintanace" type="checkbox"> 
			<label for="maintanace">Konserwacja </label>
			<input type="hidden" name="id" class="id" >
			
	</form>
</div>

<script>
$(function() {
	$( "#edit-event-keeper" ).dialog({
		autoOpen: false,
		height: 400,
		width: 350,
		modal: true,
		resizable: false,
		buttons : {
			"Ok" : function() {
				
				$.confirm({
		            'title'		: editAlert.title,
		            'message'	: editAlert.message,
		            'buttons'	: {
		                'Tak'	: {
		                    'class'	: 'blue',
		                    'action': function(){
		                    	$.post(
		            					'<?php echo Yii::app()->request->baseUrl; ?>/index.php/calendar/editKeeperReservation',
		            					$("#edit-event-form").serialize()).done(
		            					function(data) {
		            						$('#calendar').fullCalendar( 'refetchEvents' );
		            					});
		            					
		                    	$( "#edit-event-keeper" ).dialog( "close" );
		                    }
		                },
		                'Nie'	: {
		                    'class'	: 'gray',
		                    'action': function(){
		                    	$( "#edit-event-keeper" ).dialog( "close" );
			                    }	// Nothing to do in this case. You can as well omit the action property.
		                }
		            }
		        });					
				
			},	
			"Usuń":function(){
				$.confirm({
		            'title'		: deleteAlert.title_begin + deleteAlert.topic_reservation + deleteAlert.title_end,
		            'message'	: deleteAlert.message_begin + deleteAlert.topic_reservation + deleteAlert.message_end,
		            'buttons'	: {
		                'Tak'	: {
		                    'class'	: 'blue',
		                    'action': function(){	
								$.post(
										'<?php echo Yii::app()->request->baseUrl; ?>/index.php/calendar/deleteKeeperReservation',
										$("#edit-event-form").serialize()).done(
		            					function(data) {
		            						$('#calendar').fullCalendar( 'refetchEvents' );
		            					});
								$( "#edit-event-keeper" ).dialog( "close" );
		                    }
		                },
		                'Nie'	: {
		                    'class'	: 'gray',
		                    'action': function(){
		                    	$( "#edit-event-keeper" ).dialog( "close" );
			                    }	// Nothing to do in this case. You can as well omit the action property.
		                }
		            }
		        });		
			},
			"Anuluj": function() {
				$( "#edit-event-keeper" ).dialog( "close" );
			}
		},
		open: function() {
			
		},
		beforeclose: function(){ 
		    return confirm('Are you sure you want to close without saving?')
		}
	});
	var reservationDate=$('#edit-event-form .reservationdate').datepicker({ 
		dateFormat: 'dd/mm/yy',
		minDate:new Date()
	});
	$('#edit-event-form .starttime').timepicker({ 
		dateFormat: 'HH:mm',
		onSelect: function (dateText, inst) {
            var temp = $('#edit-event-form .starttime').datepicker('getDate');
       		$('#edit-event-form .starttime').datepicker('setDate', temp);
       		temp.setHours(temp.getHours());
            temp.setMinutes(temp.getMinutes()+30);
            $('#edit-event-form .endtime').datetimepicker('setDate', temp).datepicker("option", 'minDate', temp).datepicker("option", 'maxDate', temp);
       },
       onClose: function (ev){
    	   $.post(
					'<?php echo Yii::app()->request->baseUrl;?>/index.php/calendar/availableStationsAndTools',
					{
						day : getDayEdit(),
						month : getMonthEdit(),
						year : getYearEdit(),
						hourStart : getHourStartEdit(),
						hourStop : getHourStopEdit(),
						minStart : getMinuteStartEdit(),
						minStop : getMinuteStopEdit()
					},
					function(data) {
						var stations = data.stations;
						var tools = data.tools;
						showServerResponseEdit(stations, tools);
					},
					"json"
				);
       },
       hourMin: 6,
       hourMax: 21,
       hourGrid: 2,
       minuteGrid: 10
	});
	$('#edit-event-form .endtime').timepicker({ 
		dateFormat: 'HH:mm',
		onSelect: function (dateText, inst) {
			var temp = $('#edit-event-form .endtime').datepicker('getDate');
       		var temp2 = $('#edit-event-form .starttime').datepicker('getDate');
       		if(temp.getHours()<temp2.getHours()){
				temp.setHours(temp2.getHours());
				temp.setMinutes(temp2.getMinutes()+30);
           	}
       		else if(temp.getHours()==temp2.getHours()&&temp.getMinutes()<=temp2.getMinutes()){
				temp.setHours(temp2.getHours());
				var difference = temp2.getMinutes() - temp.getMinutes();
				if(difference > 0)
					temp.setMinutes(temp2.getMinutes()+difference+30);
				else
					temp.setMinutes(temp2.getMinutes()+30);
					
       		}
       		$('#edit-event-form .endtime').datepicker('setDate', temp);
       },
       onClose: function (ev){
    	   $.post(
					'<?php echo Yii::app()->request->baseUrl;?>/index.php/calendar/availableStationsAndTools',
					{
						day : getDayEdit(),
						month : getMonthEdit(),
						year : getYearEdit(),
						hourStart : getHourStartEdit(),
						hourStop : getHourStopEdit(),
						minStart : getMinuteStartEdit(),
						minStop : getMinuteStopEdit()
					},
					function(data) {
						var stations = data.stations;
						var tools = data.tools;
						showServerResponse2(stations, tools);
					},
					"json"
				);
       },
       hourMin: 6,
       hourMax: 21,
       hourGrid: 2,
       minuteGrid: 10
       });
});

function showServerResponseEdit(stations, tools)
{
	$("#edit-event-station").empty();
	for ( var i = 0; i < stations.length; i++ ) {
		$("#edit-event-station").append(
			'<option value="'+stations[i].id+'">'+stations[i].name+'</option>');
	}
	$("#edit-event-avail-tools").empty();
	for ( var i = 0; i< tools.length; i++ ) {
		$("#edit-event-avail-tools").append(
			"<input type='checkbox' name='tools[]' id='tool_" + tools[i].id + "' value='" + tools[i].id + "'>"+
			"<label class='checkboxlabel' for='tool_" + tools[i].id + "'>"+tools[i].name+"</label>");
	}
}

function getDayEdit()
{
	return $('#edit-event-form .reservationdate').datepicker( "getDate" ).getDate();
}

function getMonthEdit()
{
	return $('#edit-event-form .reservationdate').datepicker( "getDate" ).getMonth()+1;
}

function getYearEdit()
{
	return $('#edit-event-form .reservationdate').datepicker( "getDate" ).getFullYear();
}

function getHourStartEdit()
{
	return $('#edit-event-form .starttime').datepicker( "getDate" ).getHours();
}

function getMinuteStartEdit()
{
	return $('#edit-event-form .starttime').datepicker( "getDate" ).getMinutes();
}

function getHourStopEdit()
{
	return $('#edit-event-form .endtime').datepicker( "getDate" ).getHours();
}

function getMinuteStopEdit()
{
	return $('#edit-event-form .endtime').datepicker( "getDate" ).getMinutes();
}

function resetProperties()
{
	$('#edit-event-form .maintanace').prop('checked',false);
	var boxesArray = new Array(); 
	boxesArray = document.getElementsByName('tools[]'); 
	for(var i = 0; i < boxesArray.length; i++)
	{
		var checkBox = document.getElementsByName('tools[]').item(i);
		$('#edit-event-form #edit-event-avail-tools #'+checkBox.id).prop('checked', false);
	}
}
</script>
